import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Post from '../views/Post.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: () => import('@/views/Login.vue')
    },

    {
      path: '/post',
      name: 'Post',
      component: () => import('@/views/Post.vue')
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    }
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue')
    // }
  ]
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token")
  if (token || to.path == '/login') {
    next()
  } else {
    next('/login')
  }
})

export default router
